/*******************************************************************************
********************************************************************************
************************ REPLICATION FILE FOR **********************************
******************** AUER, DANIEL AND SCHAUB, MAX ******************************
*********** MASS EMIGRATION AND THE EROSIO OF LIBERAL DEMOCRACY ****************
************************** August 10, 2023 *************************************
********************************************************************************

This file replicates the analyses for tables and figures in the main
article, as well as the supplement.

The data used is proprietory. Below, the sources are listed where the data can 
be obtained, as well as details on the dataset versions used and the original
variable names.

Original Variables List
--------------------------------------------------------------------------------

ESS rounds 1-9
------------------------------------------------
European Social Survey, 2002-2018
https://www.europeansocialsurvey.org/data/
------------------------------------------------
cntry				year				ctzcntr
imueclt				imdfetn				freehms
v2x_libdem			partyvoted			gndr
vote				ctzship_all			yrbrn
marital_all			eduyrs				pspwght 
pweight				country_name		domicil 
contplt				wrkprty				wrkorg
badge 				sgnptit 			pbldmn 
bctprd 				livecnta 			livecntr 
lrscale				cntbrth 			cntbrtha 
cntbrthb 			cntbrthc 			cntbrthd 
ctzship 			ctzshipa 			ctzshipb
ctzshipc 			ctzshipd 			polintr 
brncntr 			ipcrtiv 			imprich 
ipeqopt 			ipshabt 			impsafe 
impdiff 			ipfrule 			ipudrst 
ipmodst				ipgdtim 			impfree 
iphlppl 			ipsuces 			ipstrgv
ipadvnt 			ipbhprp 			iprspot 
iplylfr 			impenv 				imptrad 
impfun



VDEM v10
------------------------------------------------
Varieties of Democracy Dataset
https://v-dem.net/data/dataset-archive/
------------------------------------------------
v2x_civlib 			v2x_polyarchy 		v2x_libdem  



MANIFESTO v2020
------------------------------------------------
Manifesto Project Dataset
Available: https://manifestoproject.wzb.eu/datasets?archived=yes
! libvote_voted (voting for a liberal party) needs to be merged with ESS
using country-specific party name/abbreviation.
------------------------------------------------
country 			year 				parfam 
per201 				per401 				per414 
per604 				pervote



SOEP v34 (pl hbrutto ppfad pgen)
------------------------------------------------
German Socio-Economic Panel (individual+household data merged on pid and syear)
Request: https://www.diw.de/en/diw_01.c.601584.en/data_access.html
pid 				cid 				hid
syear 				moved_t1 			free_speech_t0
silence_order_t0 	female 				pgpsbil_t0 
moved_before_t0		gap_t0_t1


WORDLBANK
------------------------------------------------
Worldbank Open Data
Available: https://data.worldbank.org/
------------------------------------------------
year				poprur				gdppc
gdpgrowth			pop   				tertenrol
inflation			unempl 				lifexp



GSS v7218
------------------------------------------------
General Social Survey
Available: https://gss.norc.org/Get-The-Data
------------------------------------------------
gssYear 			mobile16 			sex 
age 				marital 			educ 
letin1a 			letin1 				letin 
spkrac 				spkhomo 			thnkself 
obey 				wtssall



WVS v1.6
------------------------------------------------
World Values Sruvey Time Series, 1981 - 2020
Request: https://www.worldvaluessurvey.org/WVSDocumentationWVL.jsp
------------------------------------------------
S003 				X002_02A 			X007 
X002 				X001 				X025R 
S020 				A124_09 			A124_06 
A029 				A042 				E229



CHES v1.3
------------------------------------------------
Chapel Hill Expert Survey—Trend File, 1999 - 2019
Available: https://www.chesdata.eu/1999-2019chestrend
------------------------------------------------
country 			seat 				galtan 
year



EUROSTAT
------------------------------------------------
Available: https://data.europa.eu/data/datasets/dycibsvr4z283jjduwvdaq?locale=en
------------------------------------------------
ceepop 				cee_migrants_total
soc_exp_pc			total_emi



------------------------------------------------------------------------------*/

* Setup
********************************************************************************
clear all
set more off
set scheme tufte
graph set window fontface "Helvetica" 

*insert file path where the data is stored. Each source has its own dataset.
*global filepath "YOUR PATH"
global filepath "C:\Dropbox\Paper_democmig\submission\4_ISQ\publication\replication_materials"
* ssc install cem
* ssc install ihstrans
* ssc install grc1leg2

********************************************************************************
********************************************************************************
********************************************************************************
* ESS analyses
use "Auer_Schaub_2023ISQ_ESS_proprietory.dta", clear

g party = 0 
	replace party = 1 if partyvoted != ""
g voted = 1
	replace voted = 0 if partyvoted == ""
gen livecntr1 = year - livecnta
gen livecntr3 = 1 if livecntr1 == 0
replace livecntr3 = 2 if livecntr1 == 1 
replace livecntr3 = 2 if livecntr1 == 2
replace livecntr3 = 2 if livecntr1 == 3
replace livecntr3 = 2 if livecntr1 == 4
replace livecntr3 = 2 if livecntr1 == 5
replace livecntr3 = 3 if livecntr1 == 6
replace livecntr3 = 3 if livecntr1 == 7
replace livecntr3 = 3 if livecntr1 == 8
replace livecntr3 = 3 if livecntr1 == 9
replace livecntr3 = 3 if livecntr1 == 10
replace livecntr3 = 4 if livecntr1 == 11
replace livecntr3 = 4 if livecntr1 == 12
replace livecntr3 = 4 if livecntr1 == 13
replace livecntr3 = 4 if livecntr1 == 14
replace livecntr3 = 4 if livecntr1 == 15
replace livecntr3 = 4 if livecntr1 == 16
replace livecntr3 = 4 if livecntr1 == 17
replace livecntr3 = 4 if livecntr1 == 18
replace livecntr3 = 4 if livecntr1 == 19
replace livecntr3 = 4 if livecntr1 == 20
replace livecntr3 = 5 if livecntr1 > 20 & livecntr1 != .
replace livecntr3 = .a if livecntr1 == .
la def livecntr3 1 "Within last year" 2 "1-5 years ago" 3 "6-10 years ago" 4 "11-20 years ago" 5 "More than 20 years ago" .a "Not applicaple" .b "Refusal" .c "Don't know" .d "No answer"
egen livecntr_all = rowmax(livecntr livecntr3)
drop livecntr1 livecntr3
la var livecntr_all "How long ago first came to country"
gen female = 1 if gndr == 2
replace female = 0 if gndr == 1
g cntbrth_all = ""
foreach i in cntbrth cntbrtha cntbrthb cntbrthc cntbrthd {
	replace cntbrth_all = `i' if `i' != "" & cntbrth_all ==""
}
replace cntbrth_all = ".a" if cntbrth_all == "66"
replace cntbrth_all = ".a" if cntbrth_all == "6666"
replace cntbrth_all = ".b" if cntbrth_all == "77"
replace cntbrth_all = ".b" if cntbrth_all == "7777"
replace cntbrth_all = ".c" if cntbrth_all == "88" 
replace cntbrth_all = ".c" if cntbrth_all == "8888"
replace cntbrth_all = ".d" if cntbrth_all == "99"
replace cntbrth_all = ".d" if cntbrth_all == "9999"
replace cntbrth_all = "DE" if cntbrth_all == "01"
replace cntbrth_all = "DE" if cntbrth_all == "1000"
replace cntbrth_all = "RU" if cntbrth_all == "02"
replace cntbrth_all = "RU" if cntbrth_all == "2000"
replace cntbrth_all = "CZ" if cntbrth_all == "03"
replace cntbrth_all = "CZ" if cntbrth_all == "3000"
replace cntbrth_all = "RS" if cntbrth_all == "04" 
replace cntbrth_all = "RS" if cntbrth_all == "4000" 
replace cntbrth_all = "RS" if cntbrth_all == "06"
replace cntbrth_all = "RS" if cntbrth_all == "6000" 
foreach ctz in cntry ctzship ctzshipa ctzshipb ctzshipc ctzshipd {
	clonevar alt`ctz' = `ctz'
	replace alt`ctz' = trim(alt`ctz')
	replace alt`ctz' = "" if alt`ctz' == "66"
	replace alt`ctz' = "" if alt`ctz' == "6666"
	replace alt`ctz' = "" if alt`ctz' == "77"
	replace alt`ctz' = "" if alt`ctz' == "7777"
	replace alt`ctz' = "" if alt`ctz' == "88"
	replace alt`ctz' = "" if alt`ctz' == "8888"
	replace alt`ctz' = "" if alt`ctz' == "99"
	replace alt`ctz' = "" if alt`ctz' == "9999"
	replace alt`ctz' = "DE" if alt`ctz' == "01"
	replace alt`ctz' = "DE" if alt`ctz' == "1000"
	replace alt`ctz' = "RU" if alt`ctz' == "02"
	replace alt`ctz' = "RU" if alt`ctz' == "2000"
	replace alt`ctz' = "CZ" if alt`ctz' == "03"
	replace alt`ctz' = "CZ" if alt`ctz' == "3000"
	replace alt`ctz' = "RS" if alt`ctz' == "04" 
	replace alt`ctz' = "RS" if alt`ctz' == "4000"
	replace alt`ctz' = "RS" if alt`ctz' == "06" 
	replace alt`ctz' = "RS" if alt`ctz' == "6000" 
		forval j = 0/9 {
			 replace alt`ctz' = subinstr(alt`ctz', "`j'", "", .)
		}
}
cap drop ctzship_all
gen ctzship_all = altcntry if ctzcntr==1
foreach ctz in altctzship altctzshipa altctzshipb altctzshipc altctzshipd{
	replace ctzship_all = `ctz' if `ctz' != "" & ctzship_all ==""
}
fre ctzship_all 
gsort - ctzcntr + cntry
encode ctzship_all, gen(nctzship_all)
la var year "survey year"
la var livecntr_all "How long ago first came to live in country"
la var cntbrth_all "Country of birth"
la var ctzship_all "Citizenship"
gen refuse_lrscale = 0
replace refuse_lrscale = 1 if lrscale == .b 
gen polintr_r = 5 - polintr 
g migstatus = .
	replace migstatus = 0 if brncntr == 1 & ctzcntr == 1
	replace migstatus = 1 if brncntr == 2 & ctzcntr == 1 // Born in other ESS country 	but	 citizen of survey country
	replace migstatus = 2 if brncntr == 1 & ctzcntr == 0 // Born in surveycountry but	citizen of other ESS country
	replace migstatus = 3 if brncntr == 2 & ctzcntr == 0 // Born in other ESS country 	and	 citizen of other ESS country
la def migstatus 0 "Native (born in D, citizen D)" 1 "Naturalized (born S | citizen D)" 2 "2nd-gen. (born D | citizen S)" 3 "Migrant (born S | citizen S)" 
la val migstatus migstatus
g origin = ""
replace origin = cntry if migstatus == 0
replace origin = cntbrth_all if migstatus == 1
replace origin = ctzship_all if migstatus == 2
replace origin = cntbrth_all if migstatus == 3
gen cee=.
replace cee=1 if inlist(cntry,"BG","CZ","EE","HR","HU") | inlist(cntry,"LT","LV","PL","RO","SI","SK")
replace cee=0 if inlist(cntry,"AT","BE","DE","DK","ES") | inlist(cntry,"FI","FR","GB","GR","IE","IT")  | inlist(cntry,"LU","NL","PT","SE")
label define cee 0 "Old EU countries" 1 "CEE countries", replace
label values cee cee
label variable cee "CEE countries vs. old (pre-2004) EU countries"
gen ctzcee=.
replace ctzcee=1 if inlist(ctzship_all,"BG","CZ","EE","HR","HU") | inlist(ctzship_all,"LT","LV","PL","RO","SI","SK")
replace ctzcee=0 if inlist(ctzship_all,"AT","BE","DE","DK","ES") | inlist(ctzship_all,"FI","FR","GB","GR","IE","IT")  | inlist(ctzship_all,"LU","NL","PT","SE")
label define ctzcee 0 "Citizen of old EU country" 1 "CEE citizen", replace
label values ctzcee ctzcee
label variable ctzcee "Citizen of CEE vs. old EU country"
cap drop cce_migrant 
gen cee_migrant =1 if ctzcee==1 & ctzcntr==0
replace cee_migrant =0 if ctzcee==1 & ctzcntr==1
label variable cee_migrant "Migrant from CEE country vs. CEE resident citizen"
cap drop migrant 
gen migrant =1 if ctzcntr==0
replace migrant =0 if ctzcntr==1
label variable migrant "Migrant vs. resident citizen"
gen psharewgt=.
replace psharewgt=0.37 if cntry=="AT"
replace psharewgt=0.39 if cntry=="BE"
replace psharewgt=1.77 if cntry=="DE"
replace psharewgt=0.29 if cntry=="DK"
replace psharewgt=1.66 if cntry=="ES"
replace psharewgt=0.17 if cntry=="FI"
replace psharewgt=2.16 if cntry=="FR"
replace psharewgt=1.85 if cntry=="GB"
replace psharewgt=0.60 if cntry=="GR"
replace psharewgt=0.13 if cntry=="IE"
replace psharewgt=3.66 if cntry=="IT"
replace psharewgt=0.11 if cntry=="LU"
replace psharewgt=0.56 if cntry=="NL"
replace psharewgt=0.38 if cntry=="PT"
replace psharewgt=0.39 if cntry=="SE"
replace psharewgt=0.74 if cntry=="BG"
replace psharewgt=0.68 if cntry=="CZ"
replace psharewgt=0.10 if cntry=="EE"
replace psharewgt=1.45 if cntry=="HR"
replace psharewgt=0.74 if cntry=="HU"
replace psharewgt=0.38 if cntry=="LT"
replace psharewgt=0.54 if cntry=="LV"
replace psharewgt=2.71 if cntry=="PL"
replace psharewgt=5.06 if cntry=="RO"
replace psharewgt=0.19 if cntry=="SI"
replace psharewgt=0.69 if cntry=="SK"
cap drop rgweight
gen rgweight = psharewgt*pspwght
gen anweight1=pspwght*pweight 
replace anweight1=1 if cee_migrant==1
label variable anweight1 "Weight, CEE migrants set to 1"
tab ctzship_all if cee_migrant==1
gen migsharewgt=.
replace migsharewgt=1.56 if ctzship_all=="BG" & cee_migrant==1 
replace migsharewgt=0.72 if ctzship_all=="CZ" & cee_migrant==1 
replace migsharewgt=0.19 if ctzship_all=="EE" & cee_migrant==1 
replace migsharewgt=1.29 if ctzship_all=="HR" & cee_migrant==1
replace migsharewgt=0.55 if ctzship_all=="HU" & cee_migrant==1
replace migsharewgt=0.78 if ctzship_all=="LT" & cee_migrant==1
replace migsharewgt=1.27 if ctzship_all=="LV" & cee_migrant==1
replace migsharewgt=0.69 if ctzship_all=="PL" & cee_migrant==1
replace migsharewgt=1.57 if ctzship_all=="RO" & cee_migrant==1
replace migsharewgt=0.99 if ctzship_all=="SI" & cee_migrant==1
replace migsharewgt=0.98 if ctzship_all=="SK" & cee_migrant==1
gen anweight2=pspwght*pweight 
replace anweight2=migsharewgt if cee_migrant==1
label variable anweight2 "Weight, CEE migrants proportional to share of CEE migrants in EU15"
alpha imueclt imdfetn freehms, gen(cultlib) std reverse(imdfetn freehms)
sum cultlib
replace cultlib = (cultlib-r(min))/(r(max)-r(min))
label variable cultlib "Cultural liberalism, Kostelka and Rovny (2019)"
pca ipcrtiv imprich ipeqopt ipshabt impsafe impdiff ipfrule ipudrst ipmodst ipgdtim impfree iphlppl ipsuces ipstrgv ipadvnt ipbhprp iprspot iplylfr impenv imptrad impfun, components(3)
cap drop libval*
predict libval3 libval2 libval1 
label variable libval1 "Liberalism, principal component"
revrs ipcrtiv imprich ipeqopt ipshabt impsafe impdiff ipfrule ipudrst ipmodst ipgdtim impfree iphlppl ipsuces ipstrgv ipadvnt ipbhprp iprspot iplylfr impenv imptrad impfun
cap drop mrat
egen mrat = rowmean(revipcrtiv revimprich revipeqopt revipshabt revimpsafe revimpdiff revipfrule revipudrst revipmodst revipgdtim revimpfree reviphlppl revipsuces revipstrgv revipadvnt revipbhprp reviprspot reviplylfr revimpenv revimptrad revimpfun)
foreach val of varlist revipeqopt revipudrst revimpenv revipcrtiv revimpfree{ // the first 3 items are for universalism, the next for self-direction, which, according to Schwartz 2003, measure political liberalism
	gen c`val'= `val'-mrat
}
egen universalism = rowmean(crevipeqopt crevipudrst crevimpenv)
egen self_direction = rowmean(crevipcrtiv crevimpfree)
egen schwartz_liberalism = rowmean(universalism self_direction)
gen newage = year-yrbrn
egen std_educ = std(eduyrs)
foreach i in schwartz_liberalism female newage marital cultlib {
	egen std_`i' = std(`i')	
}
encode cntry, g(cntry_num)		
g auxyr = year * -1		
bys country_name (auxyr): carryforward cee cntry, replace
drop auxyr


********************************************************************************
* FIGURE 2
preserve
keep if ctzcntr == 1 | year < 2002
collapse (mean) cultlib schwartz_liberalism v2x_libdem (firstnm) cee, by(cntry year)
collapse (mean) cultlib schwartz_liberalism v2x_libdem, by(cee year)
drop if cee == .
foreach v in  cultlib  v2x_libdem   {
    su `v' 
    replace `v' = (`v' - r(min)) / (r(max) - r(min))
}
foreach i in cultlib v2x_libdem schwartz_liberalism {
	g `i'_eu15 = .
forvalues k = 1990(2)2018 {
	g aux1 = `i' if year == `k' & cee == 0
	egen aux2 = mean(aux1)
	replace `i'_eu15 = aux2 if year == `k'
	drop aux1 aux2
}
}
drop if cee != 1
g v2x_libdem_diff = v2x_libdem - v2x_libdem_eu15
g cultlib_diff = cultlib - cultlib_eu15
twoway  (lpoly v2x_libdem year, bw(4) lc(gs8) lp(solid)  lw(medthick)) ///
		(lpoly cultlib year , bw(4) lc(gs8)  lc(gs8) lp(longdash)  lw(medthick)) ///
		(lpoly v2x_libdem_eu15 year, bw(4) lc(black) lp(solid)  lw(medthick)) ///
		(lpoly cultlib_eu15 year , bw(4) lc(black) lp(longdash)  lw(medthick) ///
		legend(order(3 "Liberal democracy index: EU15" 4 "Cultural liberalism: EU15" 1 "Liberal democracy index: CEE" 2  "Cultural liberalism: CEE") rows(2) ring(1) pos(6) symxsize(*0.6) size(medsmall)) ///
		ylabel(0(0.2)1, nogrid format(%4.1f) labsize(medium)) xlabel(1990(6)2020,  labsize(medium)) ///
		xtitle(" ") title("A. Liberal orientation" " ", size(medlarge)) ///
		ytitle("Country Liberal Democracy Index (solid)" "Individual-level cultural liberal orientation (dash)", size(medium)) ///
		name(g1, replace)) 	
twoway (lpoly v2x_libdem_diff year, bw(4) lc(black) lp(longdash) lw(medthick)) ///
	(lpoly v2x_libdem_diff year, bw(4) lc(gs8) lp(solid) lw(medthick) alc(white) afc(gs9%25) alw(vthin)) ///
		(lpoly cultlib_diff year, bw(4) lc(gs8) lp(dash) lw(medthick) alc(white) afc(gs9%25) alw(vthin) ///
		yline(0, lc(black) lp(longdash) lw(medthick)) ///
		legend(order(1 "EU15" 2 "CEE") rows(1) ring(0) pos(7) symxsize(*0.5) size(medsmall)) ///
		ylabel(-1(0.2)0, nogrid format(%4.1f)  labsize(medium)) xlabel(1990(6)2020,  labsize(medium)) ///
		xtitle(" ") title("B. Difference EU15 vs. CEE" " ", size(medlarge)) ///
		ytitle("Difference to EU15 countries"  "Liberal dem. (solid), individual cultural liberalism (dash)", size(medium)) ///
		name(g2, replace)) 	
grc1leg  g1 g2
graph export "figure_2.eps", replace
restore


*******************************************************************
** FIGURE 4
preserve
foreach dv of varlist std_female std_newage std_schwartz_liberalism std_marital std_educ std_cultlib {
	g `dv'_migrant = cee_migrant
	eststo `dv': reg `dv' i.`dv'_migrant i.nctzship_all [pweight=anweight1]
	drop `dv'_migrant
}
coefplot std_newage std_marital std_female std_schwartz_liberalism std_educ std_cultlib  ///
		, drop(*nctzship_all _cons) ///
		msym(O) mc(black) ciopts(lc(black) lw(medthick)) yscale(noline) ///
		ylabel(,nogrid notick labsize(medlarge)) xlabel(-0.5(0.25)0.5, format(%4.2f)nogrid labsize(medlarge)) grid(none) ///
		nooffset 		xtitle(" " "Std. deviations", size(medlarge)) ///
		title("") /// title("A. Predictors of" "migrant status" "(individual CEE citizens)", size(large)) ///
		 coefl(1.std_female_migrant = "Female" ///
		 1.std_newage_migrant = "Age" ///
		 1.std_marital_migrant = "Married" ///
		 1.std_educ_migrant = "Years of education" ///
		 1.std_cultlib_migrant = "Cultural liberal orientation" ///
		 1.std_schwartz_liberalism_migrant = "Schwartz liberal attitudes") ///
		xline(0, lc(gs10)) legend(off) 
graph export "figure_4.eps", replace
restore

********************************************************************************
* FIGURE 5 + TABLE A.2
preserve
drop if cee_migrant==.
cem nctzship_all newage female  eduyrs marital cultlib, treatment(cee_migrant) k2k
orth_out newage female eduyrs marital nctzship_all cultlib if cem_matched==1, by(cee_migrant) pcompare
label variable cultlib "Cultural liberal orientation"
label variable female "Female"
label variable newage "Age in years"
global DESCVARS newage female eduyrs marital cultlib 
mata: mata clear
local i = 1
foreach var in $DESCVARS  {
    reg `var' cee_migrant if cem_matched==1 & cultlib!=.
    outreg, keep(cee_migrant)  rtitle("`: var label `var''") stats(b) ///
        noautosumm store(row`i')  starlevels(5 1) starloc(1)
    outreg, replay(diff) append(row`i') ctitles("",Difference ) ///
        store(diff) note("")
    local ++i
}
outreg, replay(diff)
local count: word count $DESCVARS
mat sumstat = J(`count',6,.)
local i = 1
foreach var in $DESCVARS {
    quietly: summarize `var' if cem_matched==1 & cee_migrant==0 & cultlib!=.
    mat sumstat[`i',1] = r(N)
    mat sumstat[`i',2] = r(mean)
    mat sumstat[`i',3] = r(sd)
    quietly: summarize `var' if cem_matched==1 & cee_migrant==1 & cultlib!=.
    mat sumstat[`i',4] = r(N)
    mat sumstat[`i',5] = r(mean)
    mat sumstat[`i',6] = r(sd)
    local i = `i' + 1
}
frmttable, statmat(sumstat) store(sumstat) sfmt(gc,fc,fc,gc,fc,fc) sdec(2)
outreg using "table_a2.tex", ///
    replay(sumstat) merge(diff) tex nocenter note("") fragment plain replace ///
    ctitles("", Matched non-migrants, "", "", Migrants, "", "", "" \ "", n, mean, sd, n, mean, sd, Diff) ///
    multicol(1,2,3;1,5,3) 
gen simulmigvote = cem_weights if cee_migrant==0
label define simulmigvote 0 "CEE residents" 1 "Migrants (predicted)", replace
label values simulmigvote simulmigvote
label variable simulmigvote "Simulated migrant vote"
global size large
reg voted i.cee_migrant	 if cee_migrant != .	
		margins cee_migrant, post
g kostelka_vote = .
	replace kostelka_vote = (55.8+60.2)/2 if nctzship_all == 17 & cee_migrant == 0 // BG
	replace kostelka_vote = (14.2+20.4)/2 if nctzship_all == 17 & cee_migrant == 1
	replace kostelka_vote =  (64.5+62.6)/2 if nctzship_all == 40 & cee_migrant == 0 // CZ
	replace kostelka_vote =  (7+7.6)/2 if nctzship_all == 40 & cee_migrant == 1
	replace kostelka_vote =  (58.2+61.9+63.5)/3 if nctzship_all == 47 & cee_migrant == 0 // EE
	replace kostelka_vote =  (7.2+6.2+13.1)/3 if nctzship_all == 47 & cee_migrant == 1
	replace kostelka_vote =  (67.8+64.4)/2 if nctzship_all == 71 & cee_migrant == 0 // HI
	replace kostelka_vote =  (5.8+4)/2 if nctzship_all == 71 & cee_migrant == 1
	replace kostelka_vote =  (46.1+48.6+52.9)/3 if nctzship_all == 97 & cee_migrant == 0 // LT
	replace kostelka_vote =  (12.8+5.2+5.2)/3 if nctzship_all == 97 & cee_migrant == 1
	replace kostelka_vote =  (61+62.6+59.4)/3 if nctzship_all == 99 & cee_migrant == 0 // LV
	replace kostelka_vote =  (14+13.3+12.8)/3 if nctzship_all == 99 & cee_migrant == 1
	replace kostelka_vote =  (40.6+53.9+48.9)/3 if nctzship_all == 128 & cee_migrant == 0 // PL
	replace kostelka_vote =  (3.9+10.3+6.6)/3 if nctzship_all == 128 & cee_migrant == 1
	replace kostelka_vote =  (58.5+39.2+41.7)/3 if nctzship_all == 134 & cee_migrant == 0 // RO
	replace kostelka_vote =  (5.1+1.1+2.4)/3 if nctzship_all == 134 & cee_migrant == 1
	replace kostelka_vote =  (63.1+65.6)/2 if nctzship_all == 144 & cee_migrant == 0 // SI
	replace kostelka_vote =  (32.1+27.4)/2 if nctzship_all == 144 & cee_migrant == 1
	replace kostelka_vote =  (54.7+58.8+59.1)/3 if nctzship_all == 145 & cee_migrant == 0 // SK
	replace kostelka_vote =  (2.2+2.7+3.2)/2 if nctzship_all == 145 & cee_migrant == 1
	replace kostelka_vote = kostelka_vote / 100
cibar kostelka_vote, over(cee_migrant) ///
	graphopts(ylabel(0 "0%" 0.1 "10%" 0.2 "20%" 0.3 "30%" 0.4 "40%" 0.5 "50%" 0.6 "60%", nogrid  labsize($size)) ///
		 xlabel(0.5 " " 1 `" "CEE residents" "(observed)" "' 2 `" "Migrants" "(observed)" "' 2.5 " ", labsize($size)) xmtick(1 2) ///
		 title("A. Voter turnout" " ", size($size)) name(voted, replace) ytitle("") legend(off)) ///
	baropts(barwidth(0.3) fcolor(gs10%75)  lcolor(gs10%75) lw(vthin)) ciopts(recast(rcap))
reg libvote_voted i.simulmigvote  if cee_migrant != .
margins simulmigvote, post
coefplot, vertical recast(bar) recast(bar) barwidth(0.3) fcolor(black%75)  lcolor(black%75) lw(vthin) ciopts(recast(rcap)) ///
		citop  ylab(0 "0%" 0.07 "7%" 0.14 "14%" 0.21 "21%" 0.28 "28%" 0.35 "35%" 0.42 "42%", nogrid labsize($size)) xlab(1 `" "CEE residents" "(observed)" "' 2 `" "Migrants" "(predicted)" "', labsize($size)) format(%9.2f) title("")  ///
		title("C. Voting for liberal parties" " ", size($size)) name(libvote_voted, replace) 
revrs contplt wrkprty wrkorg badge sgnptit pbldmn bctprd 
foreach var of varlist revcontplt revwrkprty revwrkorg revbadge revsgnptit revpbldmn revbctprd { // revpstplonl {
 recode `var' (1=0) (2=1) (.a .b .c .d = .)
}
alpha revcontplt revwrkprty revwrkorg revbadge revsgnptit revpbldmn revbctprd, gen(polengage) std
sum polengage
replace polengage = (polengage-r(min))/(r(max)-r(min))
reg polengage i.simulmigvote
margins simulmigvote, post
coefplot, vertical recast(bar) recast(bar) barwidth(0.3) fcolor(black%75)  lcolor(black%75) lw(vthin) ciopts(recast(rcap)) ///
		citop  ylab(0 "0%" 0.015 "1.5%" 0.03 "3%" 0.045 "4.5%" 0.06 "5%" 0.075 "7.5%" 0.09 "9%", nogrid labsize($size)) xlab(1 `" "CEE residents" "(observed)" "' 2 `" "Migrants" "(predicted)" "', labsize($size)) format(%9.2f) title("")  ///
		title("B. Political engagement" " ", size($size)) name(libengage, replace) 		
graph combine voted libengage libvote_voted, xsize(12) ysize(6) rows(1) // libvote_close
graph export "figure_5.eps", replace
restore


********************************************************************************
* FIGURE A.1 + TABLE A.4
* Index based on Barnea, Schwartz (2003) and Schwartz, Caprara, Vecchione (2010)
preserve
keep if ctzcntr == 1
collapse (mean) schwartz_liberalism (firstnm) cee, by(cntry year)
collapse (mean) schwartz_liberalism, by(cee year)
drop if cee == .
    su schwartz_liberalism
    replace schwartz_liberalism = (schwartz_liberalism - r(min)) / (r(max) - r(min))
	g schwartz_liberalism_eu15 = .
forvalues k = 2002(2)2018 {
	g aux1 = schwartz_liberalism if year == `k'
	egen aux2 = max(aux1)
	replace schwartz_liberalism_eu15 = aux2 if year == `k'
	drop aux1 aux2
}
drop if cee != 1
g schwartz_liberalism_diff = schwartz_liberalism - schwartz_liberalism_eu15
twoway  (lpoly schwartz_liberalism year, bw(4) lc(gs8) lp(solid)  lw(medthick)) ///
		(lpoly schwartz_liberalism_eu15 year, bw(4) lc(black) lp(dash)  lw(medthick) ///
		legend(order(2 "EU15" 1 "CEE") rows(1) ring(0) pos(7) symxsize(*0.5) size(medsmall)) ///
		ylabel(0(0.2)1, nogrid format(%4.1f) labsize(medium)) xlabel(2000(4)2020,  labsize(medium)) ///
		xtitle(" ") title("A. Schwartz liberalism" " ", size(medlarge)) ///
		ytitle("Individual universalism and self-direction", size(medium)) ///
		name(g1, replace)) 	
twoway (lpoly schwartz_liberalism_diff year, bw(4) lc(black) lp(dash)  lw(medium)) ///
	 (lpoly schwartz_liberalism_diff year, bw(4) lc(gs8) lp(solid)  lw(medthick) ///
		yline(0, lc(black) lp(dash) lw(medthick)) ///
		legend(order(1 "EU15" 2 "CEE") rows(1) ring(0) pos(7) symxsize(*0.5) size(medsmall)) ///
		ylabel(-1(0.2)0, nogrid format(%4.1f)  labsize(medium)) xlabel(2000(4)2020,  labsize(medium)) ///
		xtitle(" ") title("B. Difference EU15 vs. CEE" " ", size(medlarge)) ///
		ytitle("Individual universalism and self-direction", size(medium)) ///
		name(g2, replace)) 	
graph combine g1 g2
graph export "figure_a1.eps", replace	  
restore


********************************************************************************
* TABLE A.1
preserve
g any_migrant = .
	replace any_migrant = 0 if migrant == 0 | cee_migrant == 0
	replace any_migrant = 1 if migrant == 1 | cee_migrant == 1
global allvars any_migrant female newage marital eduyrs cultlib schwartz_liberalism voted libvote_voted
foreach i in $allvars {
	reg `i' if any_migrant == 0 
		estimates store `i'_1
	reg `i' if any_migrant == 1, robust 
		estimates store `i'_2
	reg `i' any_migrant , robust  cluster(cntry)
		estimates store `i'_3
}
foreach k in $allvars {
	if "`k'" == "any_migrant" {
		global filing "replace"
	}
	else {
		global filing "append"
	}
	esttab `k'_1 `k'_2 `k'_3 using "table_a1.tex" , $filing ///
		nostar b(3) se(3) par plain nonumbers nomtitles ///
		title("`k'") 
}
restore


********************************************************************************
* FIGURE A.3
preserve
foreach dv of varlist std_female std_newage std_marital std_educ std_cultlib std_schwartz_liberalism {
	g `dv'_migrant = migrant
	eststo `dv': reg `dv' i.`dv'_migrant i.nctzship_all if cee==0 & ctzcee==0 [pweight=anweight1]
	drop `dv'_migrant
}
coefplot std_newage std_marital std_female std_schwartz_liberalism std_educ std_cultlib ///
		, drop(*nctzship_all _cons)  name(g1, replace) ///
		msym(O) mc(black) ciopts(lc(black) lw(medthick)) yscale(noline) 	///
		ylabel(,nogrid notick labsize(medlarge)) xlabel(-0.25(0.25)0.5, format(%4.2f)nogrid  labsize(medlarge)) grid(none) nooffset ///
		xtitle(" " "Std. deviations", size(medlarge)) title(" ", size(large))	///
		 coefl(1.std_female_migrant = "Female" ///
		 1.std_newage_migrant = "Age" ///
		 1.std_marital_migrant = "Married" ///
		 1.std_educ_migrant = "Years of education" ///
		 1.std_cultlib_migrant = "Cultural liberal orientation" ///
		 1.std_schwartz_liberalism_migrant = "Schwartz liberal attitudes") ///
		xline(0, lc(gs10)) legend(off) 	
graph export "figure_a3.eps", replace
restore

********************************************************************************
* FIGURE A.4
preserve
sum newage if ctzcee==1 & cee_migrant==0 [aw=anweight1], d
sum newage if ctzcee==1 & cee_migrant==0 & newage<=45 [aw=anweight1]
sum newage if ctzcee==1 & cee_migrant==0 & newage>45 [aw=anweight1]
recode livecntr_all 1=2, gen(livecntr_all2)
gen residencelength=.
replace residencelength=1 if cee_migrant==0 & newage<=45 
replace residencelength=2 if cee_migrant==0 & newage>45
replace residencelength=3 if cee_migrant==1 & livecntr_all2==2
replace residencelength=4 if cee_migrant==1 & livecntr_all2==3
replace residencelength=5 if cee_migrant==1 & livecntr_all2==4
replace residencelength=6 if cee_migrant==1 & livecntr_all2==5
label define residencelength 1 "Resident (30y)" 2 "Resident (61y)" 3 "0-5y ago (32y)" 4 "6-10y ago (35y)" 5 "11-20y ago (39y)" 6 "20y+ (53y)", replace
label values residencelength residencelength
reg cultlib i.residencelength i.nctzship_all newage female if ctzcee==1 [pw=anweight1]
margins residencelength
marginsplot, level(90) recast(scatter) ytitle("Liberal value orientation" "(Individual sample)") xtitle(" " "Residents, migrants' length of stay, average age of group in parentheses") ///
	xlabel(1 `" "Resident" "(30y)" "' 2 `" "Resident" "(61y)" "' 3 `" "Migrant" "0-5y ago" "(32y)" "' 4 `" "Migrant" "6-10y ago" "(35y)" "' ///
				5 `" "Migrant" "11-20y ago" "(39y)" "' 6 `" "Migrant" "20y+ ago" "(53y)" "', nogrid ) title("") ///
	ylabel(, nogrid  format(%04.2f)) title("")  plotopts(mc(black) msym(o) msize(medlarge)) ciopts(recast(scatteri) lc(black) lw(medthick) plotregion(margin(medium)) xsize(11) ysize(7)) 
graph export "figure_a4.eps" , replace
restore

********************************************************************************
* FIGURE A.5
preserve
gen placeofliving=.
replace placeofliving=1 if cee_migrant==0 & domicil==5
replace placeofliving=2 if cee_migrant==0 & domicil==4
replace placeofliving=3 if cee_migrant==0 & domicil==3
replace placeofliving=4 if cee_migrant==0 & domicil==2
replace placeofliving=5 if cee_migrant==0 & domicil==1
replace placeofliving=6 if cee_migrant==1 & domicil>=3 & domicil<=5
replace placeofliving=7 if cee_migrant==1 & domicil<=2
label define placeofliving 1 `" "Resident" "Farm home" "'  2 `" "Resident" "Country village" "'   3 `" "Resident" "Town/small city" "'   4 `" "Resident" "Suburbs" "'  5   `" "Resident" "Big city" "'    6  `" "Migrants" "Countryside" "' 7 `" "Migrants" "City" "', replace
label values placeofliving placeofliving
reg cultlib i.placeofliving i.nctzship_all newage female if ctzcee==1 [aw=anweight1] // the fact that other categories (6-10, 11-20), are not different from 0-5 category speaks against socialization
margins placeofliving
marginsplot, recast(scatter) ytitle("Cultural liberalism", size(medsmall)) ///
              xtitle("Non-migrants' and migrants' place of living", size(medsmall) margin(0 0 0 5)) plotregion(margin(medium))  xlab(, nogrid labsize(small)) scheme(tufte)  title("") ///
                  ylabel(0.45(0.05)0.65, nogrid  format(%04.2f)) plotopts(mc(black) msym(o) msize(medlarge)) ciopts(recast(scatteri) lc(black) lw(medthick) plotregion(margin(medium)) xsize(11) ysize(7))
graph display, ysize(1) xsize(1.7) scale(1.1)
graph export "figure_a5.eps", replace
restore



********************************************************************************
* FIGURE A.10
preserve
reg std_cultlib i.cee_migrant##i.year std_female std_newage std_marital std_educ i.nctzship_all if cee_migrant != .
margins year, over(cee_migrant)
marginsplot, ylabel(-0.6(0.2)0.4, nogrid labsize(small)) ytitle("Predicted cultural liberal orientation" "among CEE citizens", size(small)) ///
			xtitle(" " "Survey year (number of migrants interviewed)", size(small)) ///
			ci1opts(color(black%50) lw(thin) recast(rspike)) ci2opts(color(black%50) lw(thin) recast(rspike)) title("") ///
			legend(rows(1) order(3 "CEE stayers" 4 "CEE migrants") size(small)) ///
			xlabel( 2002 `" "2002"  "(N=88)" "' ///
					2004 `" "2004"  "(118)" "' ///
					2006 `" "2006"  "(149)" "' ///
					2008 `" "2008"  "(222)" "' ///
					2010 `" "2010"  "(344)" "' ///
					2012 `" "2012"  "(322)" "' ///
					2014 `" "2014"  "(330)" "' ///
					2016 `" "2016"  "(318)" "' ///
					2018 `" "2018"  "(372)" "' , labsize(small))
graph export "figure_a10.eps", replace
restore


********************************************************************************
* FIGURE A.12
preserve
foreach dv of varlist std_female std_newage std_marital std_educ std_cultlib std_schwartz_liberalism {
	g `dv'_lib = libvote_voted
	eststo `dv': reg `dv' i.`dv'_lib i.nctzship_all [pweight=anweight1]
	drop `dv'_lib
}
coefplot std_newage std_marital std_female std_schwartz_liberalism std_educ std_cultlib ///
		, drop(*nctzship_all _cons)  name(g1, replace) ///
		msym(O) mc(black) ciopts(lc(black) lw(medthick)) yscale(noline alt) ///
		ylabel(,nogrid notick labsize(medlarge)) xlabel(-0.2(0.2)0.4, format(%4.1f)nogrid  labsize(medlarge)) grid(none) nooffset ///
		xtitle(" " "Std. deviations", size(medlarge))  ///
		title("Voting for liberal parties" " ", size(medlarge)) ///
		 coefl(1.std_female_lib = "Female" ///
		 1.std_newage_lib = "Age" ///
		 1.std_marital_lib = "Married" ///
 		 1.std_schwartz_liberalism_lib = "Schwartz liberal attitudes" ///
		 1.std_educ_lib = "Years of education" ///
		 1.std_cultlib_lib = "Cultural liberal orientation") ///
		xline(0, lc(gs10)) legend(off) 
graph export "figure_a12.eps", replace
restore


********************************************************************************
********************************************************************************
********************************************************************************
* VDEM analyses
use "Auer_Schaub_2023ISQ_VDEM_proprietory.dta", clear

encode country, g(cntry)
g aux = total_emi
	mi set mlong
	mi register imputed aux 
	mi impute poisson aux unempl lifexp gdpgrowth pop gdppc year,  by(cntry) add(10) double rseed (1516)  replace force
bys cntry year: egen imp_total_emi = mean(aux)
bys cntry year:  g auxid = _n
keep if auxid == 1
drop auxid
replace imp_total_emi = round(imp_total_emi, 1)
g log_imp_total_emi = ln(imp_total_emi+1)
g log_total_emi = ln(total_emi+1)
drop aux
global covars unempl lifexp tertenrol gdpgrowth inflation pop gdppc poprur
global outcomes v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan
g aux = year * -1
foreach i in $outcomes {
egen std_`i' = std(`i')
bys country (year): carryforward std_`i', replace
bys country (aux): carryforward std_`i', replace
}
foreach i in $covars {
bys country (year): carryforward `i', replace
bys country (aux): carryforward `i', replace
}
drop aux
g aux = year * -1
bys country (year): carryforward  year  social_liberal cons_voteshare repr_galtan ///
			v2x_civlib v2x_polyarchy v2x_libdem  social_liberal cons_voteshare  repr_galtan unempl lifexp gdpgrowth  pop gdppc, replace
bys country (aux): carryforward  year  social_liberal cons_voteshare repr_galtan ///
		v2x_civlib v2x_polyarchy v2x_libdem  social_liberal cons_voteshare  repr_galtan unempl lifexp gdpgrowth  pop gdppc, replace
drop aux
g lpop = log(pop)
replace pop = pop / 1000000
g rel_soc_exp = soc_exp_pc / gdppc

********************************************************************************
* FIGURE 1
* source: https://data.europa.eu/data/datasets/dycibsvr4z283jjduwvdaq?locale=en
preserve
g ceepop = .
		replace ceepop = 106963016 if cee == 1 & year == 2002
		replace ceepop = 106626323 if cee == 1 & year == 2003
		replace ceepop = 106335409 if cee == 1 & year == 2004
		replace ceepop = 106043440 if cee == 1 & year == 2005
		replace ceepop = 105774331 if cee == 1 & year == 2006
		replace ceepop = 105381280 if cee == 1 & year == 2007
		replace ceepop = 105003060 if cee == 1 & year == 2008
		replace ceepop = 104801616 if cee == 1 & year == 2009
		replace ceepop = 104422372 if cee == 1 & year == 2010
		replace ceepop = 104176337 if cee == 1 & year == 2011
		replace ceepop = 103936822 if cee == 1 & year == 2012
		replace ceepop = 103712512 if cee == 1 & year == 2013
		replace ceepop = 103493853 if cee == 1 & year == 2014
		replace ceepop = 103262275 if cee == 1 & year == 2015
		replace ceepop = 102992370 if cee == 1 & year == 2016
		replace ceepop = 102745400 if cee == 1 & year == 2017
		replace ceepop = 102541478 if cee == 1 & year == 2018
		replace ceepop = 102400490 if cee == 1 & year == 2019	
g cee_migrants_total = .
		replace cee_migrants_total = 1657673 if cee == 1 & year == 2002
		replace cee_migrants_total = 1844043 if cee == 1 & year == 2003
		replace cee_migrants_total = 2136866 if cee == 1 & year == 2004
		replace cee_migrants_total = 2371836 if cee == 1 & year == 2005
		replace cee_migrants_total = 2798769 if cee == 1 & year == 2006
		replace cee_migrants_total = 3421689 if cee == 1 & year == 2007
		replace cee_migrants_total = 4296762 if cee == 1 & year == 2008
		replace cee_migrants_total = 4639896 if cee == 1 & year == 2009
		replace cee_migrants_total = 4918828 if cee == 1 & year == 2010
		replace cee_migrants_total = 5221917 if cee == 1 & year == 2011
		replace cee_migrants_total = 5666983 if cee == 1 & year == 2012
		replace cee_migrants_total = 6070017 if cee == 1 & year == 2013
		replace cee_migrants_total = 6584010 if cee == 1 & year == 2014
		replace cee_migrants_total = 7189821 if cee == 1 & year == 2015
		replace cee_migrants_total = 7717248 if cee == 1 & year == 2016
		replace cee_migrants_total = 8248181 if cee == 1 & year == 2017
		replace cee_migrants_total = 8627179 if cee == 1 & year == 2018
		replace cee_migrants_total = 8701504 if cee == 1 & year == 2019		
g cee_migrants_pc = cee_migrants/ceepop
bys cee year: g ceeid = _n		
twoway (line cee_migrants_pc year if ceeid == 1, lc(black) lp(solid) lw(medthick) ///
	ytitle("CEE migrants in EU15 countries" "(share of CEE population)" " ") ///
	xtitle("") legend(off) ///
	ylabel(0 "0%" 0.02 "2%" 0.04 "4%" 0.06 "6%" 0.08 "8%" 0.1 "10%", nogrid  format(%04.3f)) ///
	xlabel(2000(4)2020, nogrid))
graph export "figure_1.eps", replace
restore


********************************************************************************
* FIGURE 6 + TABLE A.4
preserve
keep if cee == 1
replace _mi_m = 0
mi unset
xtset cntry year
sort cntry year
global fe i.year i.cntry
global covars unempl lifexp tertenrol gdpgrowth inflation pop gdppc poprur
foreach i in v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan {
g `i'_emi = log_imp_total_emi 
	xtreg f.std_`i' log_imp_total_emi $fe $covars if cee == 1
	estimates store `i'
drop `i'_emi	
	}
esttab v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan using "table_a4.tex", replace keep(log_imp_total_emi) b(3) se(3) ///
		label stats(r2 N) par plain star(* 0.10 ** 0.05 *** 0.01) 
foreach i in v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan {
	xtreg f.std_`i' log_imp_total_emi $fe if cee == 1
	estimates store `i'
	}
esttab v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan using "table_a4.tex", append keep(log_imp_total_emi) b(3) se(3) ///
		label stats(r2 N) par plain star(* 0.10 ** 0.05 *** 0.01) 
foreach i in v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan {
	g `i'_emi = log_imp_total_emi
	xtreg f.std_`i' `i'_emi $fe if cee == 1
	estimates store `i'
	drop `i'_emi
	}				
coefplot  v2x_civlib  v2x_polyarchy v2x_libdem social_liberal cons_voteshare  repr_galtan ///
			, vertical level(90)  drop($covars *.cntry *.year _cons)	 ///	
			coeflabels(, wrap(12)) rename(v2x_civlib_emi = `" "Civil" "liberties" "' ///
					v2x_libdem_emi = "Liberal democracy" ///
					v2x_polyarchy_emi ="Electoral democracy" ///
					cons_voteshare_emi = "Conservative repr." ///
					social_liberal_emi = "Social lib. repr." ///
					repr_galtan_emi ="GAL/TAN repr.") ///
			nooffset 		ytitle(" " "Std. deviations" , size(small)) ///
			title("") /// title("B. Effect of past emigration" "on liberties and representation" "(CEE countries)" " ", size(vsmall)) ///
			yline(0, lc(gs10) lw(medthin)) ///
			yscale(noline alt)  legend(off)  	msize(large)	msym(o) mc(black) ciopts(lc(black) lw(medthick)) ///
			xlabel(,nogrid angle(45) notick labsize(small)) ylabel(-0.5(0.25)0.5, format(%04.2f) notick nogrid  labsize(small)) grid(none) name(g1, replace)
			graph export "figure_6.eps", replace			
foreach i in v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan {
	xtreg f.std_`i' log_imp_total_emi i.cntry $covars if cee == 1
	estimates store `i'
	}
esttab v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan using "table_a4.tex", append keep(log_imp_total_emi) b(3) se(3) ///
		label stats(r2 N) par plain star(* 0.10 ** 0.05 *** 0.01) 
foreach i in v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan {
	xtreg f.std_`i' log_total_emi $fe $covars if cee == 1
	estimates store `i'
	}
esttab v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan using "table_a4.tex", append keep(log_total_emi) b(3) se(3) ///
		label stats(r2 N) par plain star(* 0.10 ** 0.05 *** 0.01) 
restore		


********************************************************************************
* FIGURE A.2 
preserve
g emirate = total_emi / pop
replace country = "Czechia" if country == "Czech Republic"
sort cntry year
global countries Bulgaria Croatia Czechia Estonia Hungary Latvia Lithuania Poland Romania Slovakia Slovenia
foreach i in $countries {
twoway (lowess v2x_libdem year if country=="`i'", yaxis(1) lc(red) lp(dash)) (lowess emirate year if country=="`i'", yaxis(2) lc(black) lp(solid)) ///
	, ylabel(, nogrid) xtitle(" ") title("`i'") ytitle("Emigration rate", axis(2)) ytitle("Libdem. index", axis(1)) ///
	legend(rows(2) order(1 "Liberal democracy index" 2 "Emigration / Population")) name(`i', replace)
}
grc1leg2 $countries, cols(3) ring(0) pos(4)
graph export "figure_a2.eps", replace
restore

********************************************************************************
* FIGURE A.9
preserve
bys cee year: egen av_growth = mean(gdpgrowth)
bys cee year: egen av_soc_exp = mean(soc_exp_pc)
bys cee year: egen av_rel_soc_exp = mean(rel_soc_exp)
bys cee year: g ceeid = _n
global sizing medlarge
twoway(line av_growth year if ceeid == 1 & cee == 0, lc(black) lp(solid)) ///
(line av_growth year if ceeid == 1 & cee == 1, lc(gs10) lp(solid) ///
xlabel(2000(4)2020, labsize($sizing)) xtitle("") ylabel(-10 "-10%" -5 "-5%" 0 "0%" 5 "+5%" 10 "+10%", nogrid labsize($sizing)) ytitle("Annual GDP growth", size($sizing)) ///
legend(ring(0) pos(2) order(1 "EU15" 2 "CEE") size($sizing)) name(g1, replace))
twoway(line av_rel_soc_exp year if ceeid == 1 & cee == 0, lc(black) lp(solid)) ///
(line av_rel_soc_exp year if ceeid == 1 & cee == 1, lc(gs10) lp(solid)  ///
xlabel(2000(4)2020, labsize($sizing)) xtitle("") ylabel(0 "0%" .1 "10%" .2 "20%" .3 "30%", nogrid labsize($sizing))  ytitle("Social expenditure p.c. (% of GDP)", size($sizing)) ///
legend(ring(0) pos(2) order(1 "EU15" 2 "CEE") size($sizing)) name(g2, replace))
graph combine g1 g2, xsize(7) ysize(4)
graph export "figure_a9.eps", replace
drop ceeid	
restore

********************************************************************************
* FIGURE A.13
preserve
keep if cee == 1
replace _mi_m = 0
mi unset
xtset cntry year
sort cntry year
twoway (scatter  imp_total_emi year  if country == "Bulgaria" & year >= 2002, mc(black) msym(Oh)) ///
 (scatter  total_emi year  if country == "Bulgaria" & year >= 2002, mc(gs8) msym(O) ///
 legend(order(2 "Reported" 1 "Imputed") rows(2) ring(0) pos(10) size(medlarge)) ///
 xlabel(2000 "2000" 2004 "2004" 2007 `""  " "Bulgaria" "joins EU""' 2008 "2008" 2012 "2012" 2014 `"" " "gains full" "liberties" "' 2016 "2018" 2020 "2020" , labsize(large) nogrid) ///
 ylabel(, nogrid labsize(large)) ///
 xtitle(" ") title("A. Annual emigration, Bulgaria" " ", size(large)) ytitle("Annual emigration", size(large)) ///
 xline(2007, lc(gs10) lp(dash)) xline(2014, lc(gs10) lp(solid)) ///
 name(g1, replace))
twoway (lpoly log_total_emi year if cee == 1 & year >= 2002, lc(gs8) lw(medthick) bw(2)) ///
 (lpoly log_imp_total_emi year if cee == 1 & year >= 2002, lc(black)  lw(medthick) bw(2) ///
 xlabel(2000 `""2000 " " " "  ""' 2004 "2004"  2008 "2008" 2012 "2012" 2016 "2018" 2020 "2020" , labsize(large) nogrid) ///
 ylabel(, nogrid labsize(large)) ///
 legend(order(1 "Reported" 2 "Imputed") rows(2) ring(0) pos(10) size(medlarge)) ///
 xtitle(" ") title("B. Annual emigration, all CEE countries" " " , size(large)) ytitle("Annual emigration (log)", size(large)) ///
 name(g2, replace))
graph combine g1 g2, xsize(11) ysize(6)
graph export "figure_a13.eps", replace
restore

********************************************************************************
* TABLE A.5
preserve
keep if cee == 1
replace _mi_m = 0
mi unset
xtset cntry year
sort cntry year
g emi_mil = imp_total_emi / 1000000
g emi_share = emi_mil / pop
foreach i in v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan {
	xtreg f.std_`i' emi_mil unempl lifexp tertenrol gdpgrowth rel_soc_exp inflation pop gdppc poprur if cee == 1
	estimates store `i'
	}
esttab v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan using "table_a5.tex", replace  b(3) se(3) keep(emi_mil) /// 
		label stats(r2 N) par plain star(* 0.10 ** 0.05 *** 0.01) 				
foreach i in v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan {
	xtreg f.std_`i' emi_share  unempl lifexp tertenrol gdpgrowth rel_soc_exp inflation pop gdppc poprur if cee == 1
	estimates store `i'
	}
esttab v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan using "table_a5.tex", append b(3) se(3) keep(emi_share) /// 
		label stats(r2 N) par plain star(* 0.10 ** 0.05 *** 0.01) 	
ihstrans imp_total_emi
foreach i in v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan {
	xtreg f.std_`i' ihs_imp_total_emi  unempl lifexp tertenrol gdpgrowth rel_soc_exp inflation pop gdppc poprur if cee == 1, robust
	estimates store `i'
	}
esttab v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan using "table_a5.tex", append  b(3) se(3) keep(ihs_imp_total_emi) /// 
		label stats(r2 N) par plain star(* 0.10 ** 0.05 *** 0.01) 	
restore	

********************************************************************************
* TABLE A.6	
preserve
keep if cee == 1
replace _mi_m = 0
mi unset
xtset cntry year
sort cntry year
global covars unempl lifexp tertenrol gdpgrowth rel_soc_exp inflation pop gdppc poprur
foreach i in v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan {
	xtreg f.std_`i' log_imp_total_emi $fe unempl lifexp tertenrol gdpgrowth rel_soc_exp inflation gdppc poprur lpop if cee == 1
	estimates store `i'
	}
esttab v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan using "table_a6.tex", replace keep(log_imp_total_emi) b(3) se(3) ///
		label stats(r2 N) par plain star(* 0.10 ** 0.05 *** 0.01) 
foreach i in v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan {
	ivreg2 f.std_`i' log_imp_total_emi $fe $covars  if cee == 1, partial($fe) dkraay(3)
	estimates store `i'
	}
esttab v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan using "table_a6.tex", append keep(log_imp_total_emi) b(3) se(3) ///
		label stats(r2 N) par plain star(* 0.10 ** 0.05 *** 0.01) 			
foreach i in v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan {
	xtreg f.std_`i' std_`i' log_imp_total_emi $fe if cee == 1
	estimates store `i'
	}
esttab v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan using "table_a6.tex", append keep(log_imp_total_emi std_*) b(3) se(3) ///
		label stats(r2 N) par plain star(* 0.10 ** 0.05 *** 0.01) 
xi i.year
foreach i in v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan { 
xtabond2 std_`i' l1.std_`i' l2.std_`i'  l1.log_imp_total_emi l2.log_imp_total_emi $covars  i.cntry ///
		, gmm(l1.std_`i' l2.std_`i') iv(log_imp_total_emi l1.log_imp_total_emi l2.log_imp_total_emi) nolevel robust small
estimates store `i'
}
esttab v2x_civlib v2x_polyarchy v2x_libdem social_liberal cons_voteshare repr_galtan using "table_a6.tex", append ///
	b(3) se(3) stats(r2 N) par plain star(* 0.10 ** 0.05 *** 0.01) 
restore



********************************************************************************
********************************************************************************
********************************************************************************
* GSS analyses
use "Auer_Schaub_2023ISQ_GSS_proprietory.dta", clear

********************************************************************************
* FIGURE A.7
recode mobile16 (1 = 0) (2 3 = 1), gen(mobile)
gen female = sex
egen std_female = std(female)
egen std_age = std(age)
recode marital (2 3 4 5 = 0), gen(married)
egen std_married = std(married)
egen std_edu = std(educ)
clonevar immigrants = letin1a
replace immigrants = letin if immigrants==.
replace immigrants = letin if immigrants ==.i
replace immigrants = letin1 if immigrants ==.i
revrs immigrants 
egen std_immigrant = std(revimmigrants)
revrs spkrac
egen std_racistspeak = std(revspkrac)
revrs spkhomo
egen std_homosexualspeak = std(revspkhomo)
recode thnkself (2 3 4 5 = 0), gen(thinkself)
egen std_thinkself = std(thinkself)
recode obey (2 3 4 5 = 0), gen(obeychild)
egen std_obey = std(obeychild)
foreach dv of varlist std_female std_age std_married std_edu {
	g `dv'_migrant = mobile
	eststo `dv': reg `dv' i.`dv'_migrant [pw=wtssall], absorb(gssYear)
	drop `dv'_migrant
}
coefplot std_female std_age std_married  std_edu  ///
		, drop(*nctzship_all _cons)  name(g1, replace) ///
		msym(O) mc(black) ciopts(lc(black) lw(medthick)) yscale(noline) 	///
		ylabel(,nogrid notick labsize(large)) xlabel(-0.1(0.1)0.2, format(%4.1f)nogrid  labsize(large)) grid(none) nooffset ///
		xtitle(" " "Std. deviations" " " " " "A. Migrant in the U.S.", size(large))	///
		 coefl(1.std_female_migrant = "Female" ///
		 1.std_age_migrant = "Age" ///
		 1.std_married_migrant = "Married" ///
		 1.std_edu_migrant = "Years of education")	 ///
		xline(0, lc(gs10)) legend(off) 
foreach dv of varlist std_immigrant std_racistspeak std_homosexualspeak std_thinkself std_obey {
	g mob_`dv' = mobile
	eststo `dv': reg `dv' i.mob_`dv'  [pw=wtssall], absorb(gssYear)
	drop mob_`dv'
}
coefplot std_immigrant std_homosexualspeak std_thinkself std_obey ///
		, name(g2, replace) drop (std_female std_age std_married std_edu _cons) ///
		coefl(1.mob_std_immigrant = "Allow immigration" ///
					1.mob_std_homosexualspeak = "Allow gays to speak" ///
					1.mob_std_thinkself = "Think for yourself" ///
					1.mob_std_obey = "Most important to obey")  ///
				ylabel(,nogrid notick labsize(large)) xlabel(-0.1(0.1)0.2, format(%4.1f)nogrid labsize(large)) grid(none) nooffset ///		
				xtitle(" " "Std. deviations" " " " " "B. U.S. Migrants' attitudes", size(large)) title("")  xline(0, lc(gs10)) ///
			legend(off)  yscale(noline alt)	msym(O) mc(black) ciopts(lc(black) lw(medthick)) 
graph combine g1 g2, xsize(10) ysize(6)
graph export "figure_a7.eps", replace



********************************************************************************
********************************************************************************
********************************************************************************
* SOEP analyses
use "Auer_Schaub_2023ISQ_SOEP_proprietory.dta", clear

********************************************************************************
* FIGURE A.6
egen std_free_speech_t0 = std(free_speech_t0)
egen std_silence_order_t0 = std(silence_order_t0)
gen free_speech_t0_1 = free_speech_t0/4
gen silence_order_t0_1 = silence_order_t0/4
eststo freespeach: reg moved_t1 std_free_speech_t0 female i.pgpsbil_t0 moved_before_t0 gap_t0_t1
eststo calmorder: reg moved_t1 std_silence_order_t0 female i.pgpsbil_t0 moved_before_t0 gap_t0_t1
coefplot freespeach calmorder  ///
		, name(g1, replace) drop(*origincountry _cons female *.pgpsbil_t0 moved_before_t0 gap_t0_t1) ///
		coefl(std_free_speech_t0 = "Protection of freedom of speech" ///
					std_silence_order_t0 = "Ensuring calm and order")  ///
				ylabel(,nogrid notick labsize(large)) xlabel(-0.01(0.01)0.02, format(%4.2f)nogrid labsize(medlarge)) grid(none) nooffset ///		
				xtitle(" " "Std. deviations", size(large)) title("Internal migrants in Germany" " ", size(large))  xline(0, lc(gs10)) ///
			legend(off)  yscale(noline alt)	msym(O) mc(black) ciopts(lc(black) lw(medthick)) 
graph export "figure_a6.eps", replace

********************************************************************************
********************************************************************************
********************************************************************************
* WVS analyses
use "Auer_Schaub_2023ISQ_WVS_proprietory.dta", clear

********************************************************************************
* FIGURE A.8
clonevar residencecountry = S003
clonevar birthcountry = X002_02A
* recoding of faulty Portugese observations
recode birthcountry 276=629 if residencecountry==620 & birthcountry==276
gen obs = 1
gen migrant=.
replace migrant=0 if residencecountry==birthcountry
replace migrant=1 if residencecountry!=birthcountry
gen nonmigrant=.
replace nonmigrant=0 if residencecountry!=birthcountry
replace nonmigrant=1 if residencecountry==birthcountry
clonevar origincountry = residencecountry if migrant==0
replace origincountry = birthcountry if migrant==1
g married = .
	replace married = 0 if X007 != .
	replace married = 1 if X007 == 1
bys origincountry: egen sobs = total(obs)
bys origincountry: egen snonmigrants = total(nonmigrant)
bys origincountry: egen smigrants = total(migrant)
egen std_age = std(X002)
egen std_female = std(X001)
egen std_edu = std(X025R)
egen std_married = std(married)
clonevar svyyear = S020
revrs A124_09
egen std_neighomosexual = std(revA124_09)
revrs A124_06
egen std_neighimmigrant = std(revA124_06)
egen std_childind = std(A029)
egen std_chilobedience = std(A042)
egen std_civilrights = std(E229)
foreach dv of varlist std_female std_age std_married std_edu {
	g `dv'_migrant = migrant
	eststo `dv': reg `dv' i.`dv'_migrant i.origincountry, absorb(svyyear)
	drop `dv'_migrant
}
coefplot std_female std_age std_married  std_edu  ///
		, drop(*origincountry _cons)  name(g1, replace) ///
		msym(O) mc(black) ciopts(lc(black) lw(medthick)) yscale(noline) 	///
		ylabel(,nogrid notick labsize(large)) xlabel(0 0.3, format(%4.1f)nogrid  labsize(large)) grid(none) nooffset ///
		xtitle(" " "Std. deviations", size(large)) title("A. Migrant characteristics" "(global sample)" " ", size(large))	///
		 coefl(1.std_female_migrant = "Female" ///
		 1.std_age_migrant = "Age" ///
		 1.std_married_migrant = "Married" ///
		 1.std_edu_migrant = "Years of education")	 ///
		xline(0, lc(gs10)) legend(off) 
foreach dv of varlist std_neighimmigrant std_neighomosexual std_chilobedience std_civilrights {
	g mob_`dv' = migrant
	eststo `dv': reg `dv' i.mob_`dv' i.origincountry, absorb(svyyear)
	drop mob_`dv'
}
coefplot std_neighimmigrant std_neighomosexual  std_civilrights std_chilobedience  ///
		, name(g2, replace) drop (*origincountry _cons) ///
		coefl(1.mob_std_neighimmigrant = "Accept immigrant neighbors" ///
					1.mob_std_neighomosexual = "Accept homosexual neighb." ///
					1.mob_std_chilobedience = "Child obedience" ///
					1.mob_std_civilrights = "Democracy needs civil rights")  ///
				ylabel(,nogrid notick labsize(large)) xlabel(0 0.3, format(%4.1f)nogrid labsize(large)) grid(none) nooffset ///		
				xtitle(" " "Std. deviations", size(large)) title("B. Migrants' attitudes" "(global sample)" " ", size(large)) xline(0, lc(gs10)) ///
			legend(off)  yscale(noline alt)	msym(O) mc(black) ciopts(lc(black) lw(medthick)) 		
graph combine g1 g2, xsize(10) ysize(6)
graph export "figure_a8.eps", replace



********************************************************************************
********************************************************************************
********************************************************************************
* GSS analyses
use "Auer_Schaub_2023ISQ_MANIFESTO_proprietory.dta", clear

********************************************************************************
* FIGURE A.11	
drop if year<2002
gen cee= 1 if inlist(country, 80, 81, 82, 83, 86, 87) | inlist(country, 88, 92, 93, 96, 97)
replace cee=0 if inlist(country, 11, 13, 14, 21, 22) | inlist(country, 23, 31, 32, 33, 34) | inlist(country, 35, 41, 42, 51)
recode parfam (10 20 30 50 60 70 80 90 95 98= 0) (999 = .) (40 = 1), gen(liberalparty)
label variable liberalparty "Liberal party"
foreach var of varlist per201 per401 per414 per604 {
	egen std_`var' = std(`var')
eststo `var': reghdfe liberalparty std_`var', absorb(country year) vce(robust)
}
coefplot per201 per604 per401 per414 /// 
		, msym(O) mc(black) ciopts(lc(black) lw(medthick)) yscale(noline) ///
		ylabel(,nogrid notick labsize(medium)) xlabel(-0.1(0.1)0.2, format(%4.1f)nogrid  labsize(medium)) grid(none) nooffset ///
		xtitle(" " "Std. deviations", size(medlarge)) title("", size(medium)) ///
		coefl(std_per201 = "Promoting freedom and human rights" ///
		 std_per401 = "Promoting free market economy" ///
		 std_per414 = "Promoting economic orthodoxy" /// 
		 std_per604 = "Reducing traditional morality ") ///
		xline(0, lc(gs10)) legend(off) drop(_cons) name(g1, replace)
graph export "figure_a11.eps", replace

********************************************************************************
* TABLE A.3
use "Auer_Schaub_2023ISQ_MANIFESTO_proprietory.dta" , clear
drop if year<2002
gen cee= 1 if inlist(country, 80, 81, 82, 83, 86, 87) | inlist(country, 88, 92, 93, 96, 97)
replace cee=0 if inlist(country, 11, 13, 14, 21, 22) | inlist(country, 23, 31, 32, 33, 34) | inlist(country, 35, 41, 42, 51)
g conservative = 0
	replace conservative = 1 if parfam == 60
bys country year : egen aux1 = total(pervote)
g aux2 = conservative * pervote
g aux3 = aux2 / aux1
collapse (firstnm) cee (sum) aux3, by(country year)
rename aux3 cons_voteshare
sort country year
la var cons_voteshare "Country's vote share for conservative parties (parfam=60)"

global allvars cons_voteshare
foreach i in $allvars {
	reg `i' if cee == 0 
		estimates store `i'_1
	reg `i' if cee == 1 
		estimates store `i'_2
	reg `i' cee , robust 
		estimates store `i'_3
}
foreach k in $allvars {
	esttab `k'_1 `k'_2 `k'_3 using "table_a3.tex" , replace ///
		nostar b(3) se(3) par plain nonumbers nomtitles title("`k'") 
}

use "Auer_Schaub_2023ISQ_CHES_proprietory.dta" , clear
numlabel,  add
g country_name = ""
	replace country_name = "Belgium" if country == 1
	replace country_name = "Denmark" if country == 2
	replace country_name = "Germany" if country == 3
	replace country_name = "Greece" if country == 4
	replace country_name = "Spain" if country == 5
	replace country_name = "France" if country == 6
	replace country_name = "Ireland" if country == 7
	replace country_name = "Italy" if country == 8
	replace country_name = "Netherlands" if country == 10
	replace country_name = "United Kingdom" if country == 11
	replace country_name = "Portugal" if country == 12
	replace country_name = "Austria" if country == 13
	replace country_name = "Finland" if country == 14
	replace country_name = "Sweden" if country == 16
	replace country_name = "Luxembourg" if country == 38
	replace country_name = "Bulgaria" if country ==  20
	replace country_name = "Czech Republic" if country == 21
	replace country_name = "Estonia" if country == 22
	replace country_name = "Hungary" if country == 23
	replace country_name = "Latvia" if country == 24
	replace country_name = "Lithuania" if country == 25
	replace country_name = "Poland" if country == 26
	replace country_name = "Romania" if country == 27
	replace country_name = "Slovakia" if country == 28
	replace country_name = "Slovenia" if country == 29
	replace country_name = "Croatia" if country == 31
drop if country_name == ""
g cee = 1
	replace cee = 0 if country < 19 | country == 38
bys country year: egen aux1 = sum(seat)
g aux2 = (galtan * seat) / aux1
bys country year: egen  repr_galtan = total(aux2)
collapse (firstnm) repr_galtan cee, by(country year)

global allvars repr_galtan
foreach i in $allvars {
	reg `i' if cee == 0 
		estimates store `i'_1
	reg `i' if cee == 1 
		estimates store `i'_2
	reg `i' cee , robust 
		estimates store `i'_3
}
foreach k in $allvars {
	esttab `k'_1 `k'_2 `k'_3 using "table_a3.tex" , append ///
		nostar b(3) se(3) par plain nonumbers nomtitles title("`k'") 
}


use "Auer_Schaub_2023ISQ_VDEM_proprietory.dta" , clear
global allvars v2x_civlib v2x_polyarchy v2x_libdem  social_liberal unempl lifexp gdpgrowth pop gdppc
replace pop = pop / 1000000
foreach i in $allvars {
	reg `i' if cee == 0 
		estimates store `i'_1
	reg `i' if cee == 1 
		estimates store `i'_2
	reg `i' cee , robust 
		estimates store `i'_3
}
foreach k in $allvars {
	esttab `k'_1 `k'_2 `k'_3 using "table_a3.tex" , append ///
		nostar b(3) se(3) par plain nonumbers nomtitles	title("`k'") 
}
clear all
